package com.taobao.machinesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machine_system.entity.vo.McPlanVo;
import com.taobao.machinesystem.entity.McPlan0;
import com.taobao.machinesystem.entity.McPlan1;
import com.taobao.machinesystem.entity.vo.McPlan0QueryVo;
import com.taobao.machinesystem.mapper.McPlan1Mapper;
import com.taobao.machinesystem.service.McPlan0Service;
import com.taobao.machinesystem.service.McPlan1Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 巡查计划表(从表) 服务实现类
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Service
public class McPlan1ServiceImpl extends ServiceImpl<McPlan1Mapper, McPlan1> implements McPlan1Service {

    @Autowired
    private McPlan0Service mcPlan0Service;

    @Override
    public Page<McPlan1> listMcPlan1(Integer currentPage, Integer size, McPlan0QueryVo mcPlan0QueryVo) {
        //todo 创建查询对象
        QueryWrapper<McPlan1> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("Inspect_id", mcPlan0QueryVo.getPlanId());
        //创建page对象
        Page<McPlan1> mcPlan1Page = new Page<McPlan1>(currentPage, size);

        return baseMapper.selectPage(mcPlan1Page, queryWrapper);
    }

    @Override
    public boolean updateMcPlan1(McPlan1 mcPlan1) {
        McPlan0 mcPlan0 = mcPlan0Service.getById(mcPlan1.getInspectId());
        //审核计划单
        if ("1".equals(mcPlan0.getFChecked())) {
            log.error("1 计划单已审核，不能在更改");
            throw new RuntimeException("计划单已审核，不能在更改");
        }

        baseMapper.updateById(mcPlan1);

        return true;
    }

    @Override
    public void updateState(String mcPlan1) {
        baseMapper.updateState(mcPlan1);
    }

    @Override
    public List<McPlanVo> getCurrentYearMcPlan1Ids() {
        String year = new SimpleDateFormat("YYYY").format(new Date());
        return  baseMapper.getCurrentYearMcPlan1Ids(year);
    }
}
